<?php

use luguohuakai\func\Func;

require '/usr/lib/zabbix/alertscripts/vendor/autoload.php';

$send_to = $argv[1];
$subject = $argv[2];
$message = $argv[3];
$webhook = $argv[4];

$data['msgtype'] = 'markdown';
$data['markdown']['title'] = "@$send_to $subject";
$data['markdown']['text'] = str_replace('@{ALERT.SENDTO}', "@$send_to", $message);
$data['at']['atMobiles'] = [$send_to];

$rs = Func::post($webhook, $data, [], 2);

if ($rs) {
    L($rs, 'info');
    $rss = json_decode($rs);
    if ($rss) {
        if (isset($rss->errcode) && $rss->errcode != 0) {
            L($rss->errcode . ':' . $rss->errmsg);
        }
    } else {
        L('json_decode error: ' . $rss);
    }
} else {
    L('request error: ' . $webhook);
}

function L($data, $level = 'error')
{
    Func::logs('zbx', $data, 'human-readable', FILE_APPEND, 'month', $level);
}

